import json

from fastapi import FastAPI
from drg_group.chongqing_2024.Base import DrgGroupStatus, MedicalRecord, GroupResult, groupMessages, output, DRG
from drg_group.chongqing_2024.GroupProxy import GroupProxy
from pydantic import BaseModel
import uvicorn

app = FastAPI()
drg = GroupProxy()


class rec(BaseModel):
    Index: str = ''
    age: int = None
    ageDay: int = None
    weight: float = None
    gender: int = None
    dept: str = ''
    inHospitalTime: int = None
    leavingType: str = '1'
    zdList: str = ''
    ssList: str = ''


@app.get('/test/a={a}/b={b}')
def calculate(a: int = None, b: int = None):
    c = a + b
    res = {"res": c}
    return res


@app.post('/drg_group')
def calculate(request: rec):
    print(request)
    record = MedicalRecord(Index=request.Index, age=request.age, ageDay=request.ageDay, weight=request.weight,
                           gender=request.gender, dept=request.dept, inHospitalTime=request.inHospitalTime,
                           leavingType=request.leavingType,
                           zdList=request.zdList,
                           ssList=request.ssList)
    print(record)
    result = drg.group(record)
    print(result)

    # a= json.dumps(result, ensure_ascii=False)

    a = """{"""
    a = a + '"Index":"' + str(result.Index) + '",'
    a = a + '"status":"' + str(result.status) + '",'
    a = a + '"messages":"' + str(result.messages) + '",'
    a = a + '"mdc":"' + str(result.mdc) + '",'
    a = a + '"adrg":"' + str(result.adrg) + '",'
    a = a + '"drg":"' + str(result.drg) + '"'
    a = a + '}'
    print(a)

    return a


@app.post('/drg')
def calculate(request: rec):
    record = MedicalRecord(Index=request.Index, age=request.age, ageDay=request.ageDay, weight=request.weight,
                           gender=request.gender, dept=request.dept, inHospitalTime=request.inHospitalTime,
                           leavingType=request.leavingType,
                           zdList=request.zdList,
                           ssList=request.ssList)
    result = drg.group(record)
    return result.drg


if __name__ == '__main__':
    import os  # 导入os模块

    curDirectory = os.getcwd()  # 获取更改后的工作目录。
    print(curDirectory)
    uvicorn.run(app=app,
                host="0.0.0.0",
                port=8087,
                workers=1)
